package com.zw.yzk.kabu.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.ActiveRankAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.network.entity.ActiveRankEntity;
import com.zw.yzk.kabu.internal.component.DaggerActiveRankComponent;
import com.zw.yzk.kabu.internal.module.ActiveRankModule;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.presenter.ActiveRankPresenter;
import com.zw.yzk.kabu.presenter.ActiveSearchPresenter;
import com.zw.yzk.kabu.utils.AppUtils;
import com.zw.yzk.kabu.view.view.ActiveRankView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by zhanwei on 2017/7/5.
 */

public class ActiveSearchActivity extends BaseActivity implements ActiveRankView, ActiveRankAdapter.OnItemClickedListener {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.result)
    RecyclerView result;
    @Bind(R.id.empty)
    View emptyView;

    @Inject
    ActiveSearchPresenter presenter;
    private ActiveRankAdapter adapter;
    private int votePosition;

    public static void startActiveRankActivity(Context context, int id) {
        Intent intent = new Intent(context, ActiveSearchActivity.class);
        intent.putExtra(ActivityConstant.ACTIVE_RANK_ID, id);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_active_search;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                AppUtils.popupSoftKeyboard(search);
            }
        }, 100);
        initRecyclerView();
    }

    @Override
    public void iniToolbar(BaseActivity activity) {

    }

    @Override
    public void initComponent() {
        DaggerActiveRankComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .activeRankModule(new ActiveRankModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        AppUtils.HideKeyboard(search);
    }

    @Override
    public void getActiveRankSuccess(List<ActiveRankEntity.Coser> list) {
        if (refreshLayout.isLoading()) {
            adapter.add(list);
            refreshLayout.loadMoreFinish(list.size() > 0 ? State.LOADING_SUCCEED : State.LOAD_ALL);
        } else {
            adapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
        emptyView.setVisibility(adapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void getActiveRankFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
        emptyView.setVisibility(adapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void voteSuccess() {
        adapter.voteSuccess(votePosition);
    }

    @Override
    public void onClick(ActiveRankEntity.Coser coser) {
        Intent intent = new Intent(this, CosplayDetailsActivity.class);
        intent.putExtra(ActivityConstant.OPEN_COS_DETAIL_NAME, coser.nickName);
        intent.putExtra(ActivityConstant.OPEN_COS_DETAIL_ID, coser.cosId);
        startActivity(intent);
    }

    @Override
    public void onVote(ActiveRankEntity.Coser coser, int position) {
        votePosition = position;
        presenter.vote(coser.cosId);
    }

    @OnClick(R.id.cancel)
    public void cancel() {
        finish();
    }

    @OnTextChanged(value = R.id.search, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        getActive(0, s.toString());
    }

    /**
     * 初始化recyclerview
     */
    private void initRecyclerView() {
        result.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
        adapter = new ActiveRankAdapter(null);
        adapter.setOnItemClickedListener(this);
        result.setAdapter(adapter);

        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getActive(0, search.getText().toString());
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getActive(adapter.getItemCount(), search.getText().toString());
                    }
                }).build();
    }

    /**
     * 获取活动
     * @param start
     */
    public void getActive(int start, String key) {
        presenter.getActiveRank(getIntent().getIntExtra(ActivityConstant.ACTIVE_RANK_ID, -1), start, key);
    }
}
